$(function(){
    //////////---------------发布动态-----------------//////////////////////
    var $public_dynamic = $("a:contains('发布动态')");
    
    $public_dynamic.click(function(){
        var dynamic_information_content = $("#form-field-11").val();
        var dynamic_photo_path = "";
        if(dynamic_information_content!=""&&dynamic_information_content!=null){
        $.ajax({
            url:"Publish_dynamic",
            type:"POST",
            data:{dynamic_information_content:dynamic_information_content,dynamic_photo_path:dynamic_photo_path},
            async:false,
            dataType:"json",
            success:function(data,status){
                if(data.result==true){
                    jSuccess("发布动态成功",{HorizontalPosition : "center",VerticalPosition : "center",ShowOverlay : false}); 
                    $("#form-field-11").val("");
                    window.location.reload();
                }
                else{
                    jError("操作失败",{HorizontalPosition : "center",VerticalPosition : "center",ShowOverlay : false});
                }
            }
        });
    }else{
    	jError("发布类容不能为空",{HorizontalPosition : "center",VerticalPosition : "center",ShowOverlay : false});
    }
    });
});



//////////////////////-------用户头像加载-----------//////////

$(function(){
	var $write_user = $("div.write_user");
	
	$.get("User_jurisdiction_servlet",function(data,status){
		if(data.result){
			$write_user.children("span").html(data.nickname);
			if(data.user_photo_path!=""&&data.user_photo_path!=null){
				$write_user.children("img")[0].src=data.user_photo_path;
			}else{
				$write_user.children("img")[0].src="images/no_photo.jpg";
			}
		}
	},
	"json"
	);
});




//--------------点赞--加关注--转发------------////////////
$(function(){
	var $timeline_items = $("div[class='timeline-items hidden']");           //动态页面单个模块
    var $attention = $timeline_items.find("i.attention");
    var $praise = $timeline_items.find("i.praise")
    var $forword = $timeline_items.find("i.forword")
    
    
    //加关注
    $attention.click(function(){
    	var $attention_number = $(this).siblings("span.attention");
    	var attention_number = parseInt($attention_number.html());
    	var $dynamic = $(this).parents("div[class='timeline-items']");
    	$.ajax({
    		url:"Attention",
    		type:"POST",
    		async:false,
    		data:{dynamic_information_id:$dynamic[0].id},
    		dataType:"json",
    		success:function(data,status){
    			if(data.result==true){
    				 if(data.attentioned==true){
    				 $attention_number.html(attention_number+1);
    				 }else{
    					 jError("您已经关注过该动态了",{HorizontalPosition : "center",VerticalPosition : "center",ShowOverlay : false});
    				 }
    			}else{
    				jError("操作失败",{HorizontalPosition : "center",VerticalPosition : "center",ShowOverlay : false});
    			}
    		}
    	});
    });
    
    //点赞
    $praise.click(function(){
    	var $praise_number = $(this).siblings("span.praise");
    	var praise_number = parseInt($praise_number.html());
    	var $dynamic = $(this).parents("div[class='timeline-items']");
    	
    	$.ajax({
    		url:"Praise",
    		type:"POST",
    		async:false,
    		data:{dynamic_information_id:$dynamic[0].id},
    		dataType:"json",
    		success:function(data,status){
    			if(data.result==true){
    				 if(data.praise==true){
    					 $praise_number.html(praise_number+1);
    				 }else{
    					 jError("您已经点过赞了",{HorizontalPosition : "center",VerticalPosition : "center",ShowOverlay : false});
    				 }
    			}else{
    				jError("操作失败",{HorizontalPosition : "center",VerticalPosition : "center",ShowOverlay : false});
    			}
    		}
    	});
    });
    
    
});

////////////////////////////-------------------加载动态模块/发布评论--------------------///////////////////////////

$(function(){
	var $timeline_container = $("div[class='timeline-container publish']");            //动态页面总模块
	var $timeline_items = $("div[class='timeline-items hidden']");           //动态页面单个模块
	var $timeline_label_my = $timeline_container.children("div.timeline-label");
	
	$.ajax({
		url:"My_dynamic",
		type:"POST",
		async:false,
		dataType:"json",
		success:function(data,status){
			for(i=0;i<data.length;i++){
				var $timeline_item_c = $timeline_items.clone(true);      //复制元素
			
				if(data[i].user_photo_path == ""||data[i].user_photo_path == null){
					$timeline_item_c.find("div.timeline-info").children("img")[0].src = "images/no_photo.jpg";
				}else{
					$timeline_item_c.find("div.timeline-info").children("img")[0].src=data[i].user_photo_path;
				}
				$timeline_item_c[0].id=data[i].dynamic_information_id;
				$timeline_item_c.find("div.timeline-info").children("span").html(data[i].nickname);
				$timeline_item_c.find("div.widget-header").children("h5").children("span").html(data[i].dynamic_information_date);
				$timeline_item_c.find("div[class='widget-main']").children("span").html(data[i].dynamic_information_content);
				$timeline_item_c.find("span.attention").html(data[i].dynamic_information_attention_number);
				$timeline_item_c.find("span.praise").html(data[i].dynamic_information_praise_number);
				$timeline_item_c.find("span.review").html(data[i].dynamic_information_review_number);
				$timeline_item_c.find("span.forword").html(data[i].dynamic_information_forword_number);
				
				//评论
				var $review = $timeline_item_c.find("div.dialogs");  //待前台完成
				var $publish_r = $timeline_item_c.find("form");
				
				for(j=0;j<data[i].review_Array.length;j++){
					var $review_c = $review.clone(true);
					$review_c[0].id = data[i].review_Array[j].review_id;
					
					if(data[i].review_Array[j].reviewer_photo == ""||data[i].review_Array[j].reviewer_photo == null){
						$review_c.find("div.user").children("img")[0].src = "images/no_photo.jpg";
					}else{
						$review_c.find("div.user").children("img")[0].src=data[i].user_photo_path;
					}
					$review_c.find("span.green").html(data[i].review_Array[j].review_date);
					$review_c.find("a.reviewer_nickname").html(data[i].review_Array[j].reviewer_nickname);
					$review_c.find("div.text").html(data[i].review_Array[j].review_content);
					
					$review_c.removeClass("hidden");
					$review_c.insertBefore($publish_r);
				}
				
				
				
				$timeline_item_c.removeClass("hidden");
				$timeline_item_c.insertAfter($timeline_label_my);
				
				//first----发布评论
				var $publish_review = $timeline_item_c.find("button[class='btn btn-sm btn-info no-radius']");
				$publish_review.click(function(){
					//var $review_content = $timeline_item_c.find("input[class='form-control myreview']");      //在不断的循环中click里面的代码并没有被执行，而$timeline_item_c所指向值一直在变化，最后一次就指向了最后一个对象,所以click始终获取到最后一个input的值，和id
					var $review_content = $(this).parent().siblings("input.myreview") ;
					var review_content = $review_content.val();   //发表的类容
					var dynamic_information_id = $(this).parents("div[class='timeline-items']")[0].id;
					
					//var $review_copy = $(this).parents("form").siblings("div[class='dialogs hidden']").clone(true);
					if(review_content!=""&&review_content!=null){
					 $.ajax({
						 url:"Publish_review",
						 type:"POST",
						 data:{dynamic_information_id:dynamic_information_id,review_content:review_content},
						 async:false,
						 dataType:"json",
						 success:function(data,status){
							 if(data.result==true){
				                    jSuccess("评论成功",{HorizontalPosition : "center",VerticalPosition : "center",ShowOverlay : false}); 
				                    $review_content.val("");
				                    window.location.reload();
				                }
				                else{
				                    jError("操作失败",{HorizontalPosition : "center",VerticalPosition : "center",ShowOverlay : false});
				                }
						 }
						 
					 });
				}else{
					jError("评论类容不能为空",{HorizontalPosition : "center",VerticalPosition : "center",ShowOverlay : false});
				}
					 
				});
				//end!----发布评论
				
			}
		}
	});
});























